

export function createPoint(width,height){

    var vx = (Math.random()-0.5);
    var  vy = (Math.random()-0.5);
    var  r = ~~(Math.random()*5);
    var  x = ~~(Math.random()*(width-r));
    var  y =~~(Math.random()*(height-r));
    var point={
      x:x,
      y:y,
      vx:vx,
      vy:vy,
      r:r
    }
     return(point);
  }

//绘制点和点的线
export function drawLine(ctx, p1x, p1y, p2x, p2y) {
    var xDistance = Math.abs(p1x - p2x);//计算两点间的x距离
    var yDistance = Math.abs(p1y - p2y);//计算两点间的y距离
    var distance = Math.sqrt(xDistance * xDistance + yDistance * yDistance);
    if (distance <= 120) {
        ctx.fillStyle = '#ffffff';//解决窗口缩放时圆点变黑
        ctx.strokeStyle = 'rgba(255,255,255,' + (1 - distance / 120) + ')';//线的透明度根据距离变化 越远越淡
        ctx.beginPath();//注意一定要有
        ctx.moveTo(p1x, p1y);
        ctx.lineTo(p2x, p2y);
        ctx.stroke();
    }
}


//清空画布
export function clearCanvas(ctx,canvas)  
{  
    ctx.clearRect(0,0,canvas.width,canvas.height);  
} 
//计算下一点
export function calcNext(pointArray,canvas,width,height){
  for(var i = 0;i<100;i++){
    var p = pointArray[i];
    p.x = p.x+p.vx;
    p.y = p.y+p.vy;
    if(p.x<0 || p.x>canvas.width || p.y<0 || p.y>canvas.height){//点的再生
      p.x = ~~(Math.random()*(width-p.r));
      p.y =~~(Math.random()*(height-p.r));
    }
  }
}
//话电话线
export function dpdl(ctx,pointArray,MouseX,MouseY,drawLine){
  pointArray.forEach(point => {
    ctx.beginPath()
    ctx.arc(point.x,point.y,point.r,0,Math.PI*2);
    ctx.fill() 
    //ctx.closePath()//closePath()和路径结束冇关系
  });
  for(var i = 0;i<199;i++){
    var point1=pointArray[i];
    drawLine(ctx,MouseX,MouseY,point1.x,point1.y);
    for(var j=i+1;j<200;j++){
      var point2=pointArray[j];
      drawLine(ctx,point1.x,point1.y,point2.x,point2.y);
    }
  }
}


/*

let start;
let now=performance.now();
function step(timestamp) {
  if (start === undefined)
    start = timestamp;
  clearCanvas();
  calcNext();
  dpdl();
    window.requestAnimationFrame(step);
}

window.requestAnimationFrame(step);
*/